import datetime
from time import sleep

from common.util import questionnaireDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#登录测试
class register:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = questionnaireDriver.driver
        self.driver.get(self.url)

#正确用例
    def register_test01(self):
        user_name = "zhangsan" + datetime.datetime.now().strftime("%H%M%S")
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys(user_name)
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(user_name)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("Aa123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("Aa123456")

        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()

        text = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body > div:nth-child(7) > div > div > div > div > div > span:nth-child(2)")))

        questionnaireDriver.screenshot()
        assert text.text == "注册成功"

#错误用例
    #均为空值
    def register_test02(self):
        self.driver.back()

        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()


        name = self.driver.find_element(By.CSS_SELECTOR, "#name_help > div")
        user_name = self.driver.find_element(By.CSS_SELECTOR, "#username_help > div")
        password = self.driver.find_element(By.CSS_SELECTOR, "#password_help > div")
        re_password = self.driver.find_element(By.CSS_SELECTOR, "#rePassword_help > div")

        questionnaireDriver.screenshot()
        assert name.text == "用户名是必填项！"
        assert user_name.text == "登录账号不能为空"
        assert password.text == "密码是必填项！"
        assert re_password.text == "请再次输入密码"


    # 登录账号重复
    def register_test03(self):
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("Aa123456")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("Aa123456")
        self.driver.find_element(By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button > span").click()

        text = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div")))

        questionnaireDriver.screenshot()
        assert text.text == "账号已存在"

    #两次密码不一致
    def register_test04(self):
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("1")

        text = WebDriverWait(self.driver, 3).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#rePassword_help > div")))

        questionnaireDriver.screenshot()
        assert text.text == "两次密码不一致!"

